package com.tadpole.music.presenter.me;

import com.tadpole.music.bean.me.MemberCenterBean;
import com.tadpole.music.callback.BaseCallback;
import com.tadpole.music.config.Constant;
import com.tadpole.music.config.UrlConfig;
import com.tadpole.music.iView.me.MemberCenterIView;
import com.tadpole.music.model.base.DataModel;
import com.tadpole.music.model.base.ModelToken;
import com.tadpole.music.presenter.base.BasePresenter;
import com.tadpole.music.utils.JsonParseUtil;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.utils.ToastUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 会员中心
 * Created By 孙岩
 * on 2020/9/21
 */
public class MemberCenterPresenter extends BasePresenter<MemberCenterIView> {
    @SuppressWarnings("unchecked")
    public void getMemberCenter() {
        String url = UrlConfig.getMemberCenter;
        String token = SpUtil.getInstance(getView().getContext()).getString(Constant.TOKEN, "");
        DataModel.request(ModelToken.GET_HEAD_REQUEST)
                .url(url)
                .token(token)
                .execute(new BaseCallback<String>() {
                    @Override
                    public void onSuccess(String data) {
                        try {
                            JSONObject obj = new JSONObject(data);
                            if (obj.getInt("code") == 200) {
                                List<MemberCenterBean.DataBean> list = new ArrayList<>();
                                JSONArray array = new JSONArray(obj.getString("data"));
                                for (int i = 0; i < array.length(); i++) {
                                    JSONObject object = new JSONObject(array.get(i).toString());
                                    List<MemberCenterBean.DataBean.GoodsBean> goodsBeanList = new ArrayList<>();
                                    JSONArray jsonArray = new JSONArray(object.getString("goods"));
                                    for (int j = 0; j < jsonArray.length(); j++) {
                                        JSONObject jsonObject = new JSONObject(jsonArray.get(j).toString());

                                        JSONObject rankObj = new JSONObject(jsonObject.getString("rank"));
                                        MemberCenterBean.DataBean.GoodsBean.RankBean rankBean = new MemberCenterBean.DataBean.GoodsBean.RankBean(
                                                JsonParseUtil.getInt(rankObj, "id"),
                                                JsonParseUtil.getStr(rankObj, "name")
                                        );

                                        JSONObject collegeObj = new JSONObject(jsonObject.getString("college"));
                                        MemberCenterBean.DataBean.GoodsBean.CollegeBean collegeBean = new MemberCenterBean.DataBean.GoodsBean.CollegeBean(
                                                JsonParseUtil.getInt(collegeObj, "id"),
                                                JsonParseUtil.getStr(collegeObj, "created_at"),
                                                JsonParseUtil.getStr(collegeObj, "updated_at"),
                                                JsonParseUtil.getStr(collegeObj, "name")
                                        );



                                        MemberCenterBean.DataBean.GoodsBean goodsBean = new MemberCenterBean.DataBean.GoodsBean(
                                                JsonParseUtil.getInt(jsonObject, "id"),
                                                JsonParseUtil.getStr(jsonObject, "name"),
                                                JsonParseUtil.getStr(jsonObject, "thumb"),
                                                JsonParseUtil.getStr(jsonObject, "app_thumb"),
                                                JsonParseUtil.getStr(jsonObject, "desc"),
                                                JsonParseUtil.getInt(jsonObject, "college_id"),
                                                collegeBean,
                                                JsonParseUtil.getDouble(jsonObject, "price"),
                                                JsonParseUtil.getBoolean(jsonObject, "paid"),
                                                rankBean
                                        );
                                        goodsBeanList.add(goodsBean);
                                    }

                                    MemberCenterBean.DataBean dataBean = new MemberCenterBean.DataBean(
                                            JsonParseUtil.getStr(object, "name"),
                                            JsonParseUtil.getStr(object, "thumb"),
                                            JsonParseUtil.getInt(object, "college_id"),
                                            goodsBeanList
                                    );

                                    list.add(dataBean);
                                }
                                getView().showMemberCenter(list);

                            } else if (obj.getInt("code") == 401) {
                                getView().show401();
                            } else {
                                ToastUtils.show(obj.getString("msg"));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(String msg) {
                        getView().showErr();
                        getView().stopRefresh();
                    }

                    @Override
                    public void onComplete() {
                        getView().hideLoading();
                        getView().stopRefresh();
                    }

                    @Override
                    public void onBefore() {
                        getView().showLoading();
                    }
                });
    }
}
